package flc.ast.utils;

import android.graphics.Canvas;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.adapter.AlbumAdapter;
import flc.ast.adapter.AlbumAdapter1;
import flc.ast.bean.MyAlbumBean;
import flc.ast.fragment.HomeFragment;


public class CardViewHelperCallback extends ItemTouchHelper.Callback {
    public static final String TAG = CardViewHelperCallback.class.getSimpleName();

    private List<MyAlbumBean> mDatas;
    private List<MyAlbumBean> leftDatas = new ArrayList<>();
    private List<MyAlbumBean> rightDatas = new ArrayList<>();
    private AlbumAdapter1 mAdapter;

    public CardViewHelperCallback(List<MyAlbumBean> mDatas, AlbumAdapter1 mAdapter) {
        this.mDatas = mDatas;
        this.mAdapter = mAdapter;
    }

    public CardViewHelperCallback() {
    }

    public void setmDatas(List<MyAlbumBean> mDatas) {
        this.mDatas = mDatas;
    }

    public void setmAdapter(AlbumAdapter1 mAdapter) {
        this.mAdapter = mAdapter;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        Log.d(TAG, "getMovementFlags called!");
        MyAlbumBean bean =
                (MyAlbumBean) recyclerView
                        .getChildAt(recyclerView.getChildCount() - 1)
                        .getTag();
        MyAlbumBean currentDrawBean =
                (MyAlbumBean) viewHolder.itemView.getTag();

        if (currentDrawBean.getIndex() != bean.getIndex()) {
            return makeMovementFlags(0, 0);
        }
        if (1 == bean.getIndex()) {
            return makeMovementFlags(0
                    , ItemTouchHelper.LEFT
            );
        } else if (bean.getIndex() < recyclerView.getChildCount()) {
            return makeMovementFlags(0
                    ,
                    ItemTouchHelper.LEFT
                            | ItemTouchHelper.RIGHT
            );
        } else {
            return makeMovementFlags(0
                    ,
                    ItemTouchHelper.RIGHT
            );
        }


    }

    /**
     * 是否启用滑动操作
     *
     * @return 是否启用 true 启用, false 不启用
     */
    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        Log.i(TAG, "onSwiped called ！direction = " + direction);
        if (mDatas.size()<=viewHolder.getLayoutPosition()){
            return;
        }
        MyAlbumBean s = this.mDatas.remove(viewHolder.getLayoutPosition());
        mDatas.add(0, s);
        switch (direction) {
            case ItemTouchHelper.LEFT:
                s.setDirection(ItemTouchHelper.LEFT);
                leftDatas.add(0, s);
                if(rightDatas.size()>0){
                    MyAlbumBean right=rightDatas.remove(0);
                    mDatas.remove(right);
                    mDatas.add(right);
                }
                break;
            case ItemTouchHelper.RIGHT:
                s.setDirection(ItemTouchHelper.RIGHT);
                rightDatas.add(0,s);

                if (leftDatas.size() > 0){
                    MyAlbumBean left=leftDatas.remove(0);
                    mDatas.remove(left);
                    mDatas.add(left);
                }
                break;
            default:
                break;
        }
        mAdapter.setSelPosition(viewHolder.getLayoutPosition());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView
            , @NonNull RecyclerView.ViewHolder viewHolder
            , float dX, float dY, int actionState, boolean isCurrentlyActive) {
        //计算移动距离
        float distance = (float) Math.hypot(dX, dY);
        float maxDistance = recyclerView.getWidth() / 2f;

        int currentDrawIndex = ((MyAlbumBean) viewHolder.itemView.getTag()).getIndex();

        //比例
        float fraction = distance / maxDistance;
        if (fraction > 1) {
            fraction = 1;
        }
        //为每个child执行动画
        int count = recyclerView.getChildCount();

        for (int i = 0; i < count; i++) {
            //获取的view从下层到上层
            View view = recyclerView.getChildAt(i);

            int level = CardConfig.SHOW_MAX_COUNT - i - 1;
            //level范围（CardConfig.SHOW_MAX_COUNT-1）-0，每个child最大只移动一个CardConfig.TRANSLATION_Y和放大CardConfig.SCALE
            MyAlbumBean bean = (MyAlbumBean) view.getTag();

            if (level == CardConfig.SHOW_MAX_COUNT - 1) { // 最下层的不动和最后第二层重叠
                view.setTranslationX(CardConfig.TRANSLATION_X * (level - 1));
                view.setScaleX(1 - CardConfig.SCALE * (level - 1));
                view.setScaleY(1 - CardConfig.SCALE * (level - 1));
            } else if (level > 0) {
//                if (currentDrawIndex==bean.getIndex()&&bean.getDirection() == ItemTouchHelper.LEFT) {
//                    view.setTranslationX(level * CardConfig.TRANSLATION_X + fraction * CardConfig.TRANSLATION_X);
//                } else if (currentDrawIndex==bean.getIndex()&&bean.getDirection() == ItemTouchHelper.RIGHT) {
//                    view.setTranslationX(level * CardConfig.TRANSLATION_X - fraction * CardConfig.TRANSLATION_X);
//                } else {
//                    view.setTranslationX(level * CardConfig.TRANSLATION_X - fraction * CardConfig.TRANSLATION_X);
//                }
                view.setScaleX(1 - level * CardConfig.SCALE + fraction * CardConfig.SCALE);
                view.setScaleY(1 - level * CardConfig.SCALE + fraction * CardConfig.SCALE);
            }
        }

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
        return 0.3f;
    }
}
